// loan/loan.js
let totalLoansAvailable = 900000; // 贷款总额最大值
let proportion = 1.1; // 贷款总额比例
const utils = require("../../../../utils/util");
let toggleCollection = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loanType: ["等额本息", "等额本金"], // 贷款类型
    selectType: ["贷款", "缴费", "供房"], // 查询类型
    selectIndex: "0",
    templateName: "loan",
    // 模板数据
    template: {
      self: "0.00", // 公积金个人缴纳
      comp: "0.00", // 公积金单位缴纳
      result: "0.00", // 最上方显示金额
      booleanType: ["是", "否"],
      unextractedIndex: 0,
      jointApplicantIndex: 1,
      complateType: ["等额本金", "等额本息"],
      complateIndex: "0",
      showNum: false,
      showYear: false,
      scroll: false,
    },
  },
  onLoad: function (options) {
    // utils.colleation(this, "公积金计算器");
    toggleCollection = true;
    if (options.selectIndex)
      this.data.selectIndex = options.selectIndex;
    this.setData({
      selectIndex: this.data.selectIndex,
    });
    if (this.data.selectIndex === "0")
      this.data.templateName = "loan"
    else if (this.data.selectIndex === "1")
      this.data.templateName = "pay"
    else if (this.data.selectIndex === "2")
      this.data.templateName = "donorRoom"
    this.setData({
      templateName: this.data.templateName,
    });
  },
  // 查询类型
  selectTypeChange(e) {
    let index = e.detail.value;
    let templateName;
    switch (index) {
      case "0":
        templateName = "loan";
        break;
      case "1":
        templateName = "pay";
        break;
      case "2":
        templateName = "donorRoom"
        break;
      default:
        templateName = "loan";
    }
    let result = "template.result";
    this.setData({
      selectIndex: index,
      templateName,
      [result]: "0.00"
    });
  },
  // 三年内未提取
  unextractedYears(e) {
    let index = e.detail.value;
    let booleanIndex = "template.unextractedIndex";
    if (index === "0")
      proportion = 1.1;
    else if (index === "1")
      proportion = 1;
    this.setData({
      [booleanIndex]: index,
    });
  },
  // 共同申请人
  jointApplicant(e) {
    let index = e.detail.value;
    let jointApplicantIndex = "template.jointApplicantIndex";
    if (index === "1")
      totalLoansAvailable = 500000;
    else if (index === "0")
      totalLoansAvailable = 900000;
    this.setData({
      [jointApplicantIndex]: index,
    });
  },
  // 计算方式
  complateType(e) {
    let index = e.detail.value;
    let complateIndex = "template.complateIndex";
    this.setData({
      [complateIndex]: index,
    });
  },
  // 贷款计算
  calculationLoan(e) {
    let balance = parseInt(e.detail.value.balance);
    let spouseBalance = parseInt(e.detail.value.spouseBalance ? e.detail.value.spouseBalance : 0);
    if (balance) {
      let result = ((balance * 14) + (spouseBalance * 14)) * proportion;
      this.data.template.result = (result <= totalLoansAvailable ? result : totalLoansAvailable).toFixed(2);
      this.setData({
        template: this.data.template,
      });
    } else
      wx.showToast({
        title: '请输入公积金余额',
        icon: 'none',
      });
  },
  // 贷款重置
  resetLoan(e) {
    this.data.template.result = "0.00";
    this.setData({
      template: this.data.template,
    });
  },
  // 计算缴费
  calculationPay(e) {
    let base = parseInt(e.detail.value.base);
    let proportion = parseInt(e.detail.value.proportion);
    if (base && proportion) {
      if (base < 2200 || base > 27927) {
        wx.showToast({
          title: '缴存基数范围为：2200-27927',
          icon: 'none',
        });
        return;
      } else if (proportion < 5 || proportion > 12) {
        wx.showToast({
          title: '缴存比例范围为：5%-12%',
          icon: 'none',
        });
        return;
      } else {
        this.data.template.result = (base * proportion * 2 / 100).toFixed(2);
        this.data.template.self = (base * proportion * 2 / 100 * 0.5).toFixed(2);
        this.data.template.comp = this.data.template.self;
        this.setData({
          template: this.data.template,
        });
      }
    } else {
      if (!base) {
        wx.showToast({
          title: '请输入缴存基数',
          icon: 'none',
        });
        return;
      } else if (!proportion) {
        wx.showToast({
          title: '请输入缴存比例',
          icon: 'none',
        });
        return;
      }
    }
  },
  // 缴费重置
  resetPay(e) {
    this.data.template.result = "0.00";
    this.data.template.self = "0.00";
    this.data.template.comp = "0.00";
    this.setData({
      template: this.data.template,
    });
  },
  // 计算供房
  complateDonorRoom(e) {
    let year = parseInt(e.detail.value.year);
    let loanTotal = parseFloat(e.detail.value.loanTotal);
    if (year && loanTotal) {
      if (year < 0 || year > 30) {
        wx.showToast({
          title: '贷款年限的上限为30年，请修改。',
          icon: 'none',
        });
        return;
      } else if (loanTotal < 0 || loanTotal > 90) {
        wx.showToast({
          title: '贷款金额的上限为90万元，请修改。',
          icon: 'none',
        });
        return;
      } else {
        wx.navigateTo({
          url: `../result/result?year=${year}&loanTotal=${loanTotal}&type=${this.data.template.complateIndex}`,
        });
      }
    } else if (!year) {
      wx.showToast({
        title: '请输入贷款年限',
        icon: 'none',
      });
      return;
    } else if (!loanTotal) {
      wx.showToast({
        title: '请输入贷款金额',
        icon: 'none',
      });
      return;
    }

  },
  showtapsYear() {
    let showYear = "template.showYear";
    this.setData({
      [showYear]: true,
    });
    setTimeout(() => {
      this.setData({
        [showYear]: false,
      });
    }, 1700);
  },
  showtapsNum() {
    let shwoNum = "template.showNum";
    this.setData({
      [shwoNum]: true,
    });
    setTimeout(() => {
      this.setData({
        [shwoNum]: false,
      });
    }, 1700);
  },
  onShareAppMessage: function () {
    return {
      imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
    }
  },
  onPageScroll() {
    if (toggleCollection) {
      this.setData({
        scroll: true,
      });
      toggleCollection = false;
    }

  },
  closeCollection() {
    toggleCollection = false;
  },
  openCollection() {
    toggleCollection = true;
  }
})